﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using WCS.Utils;
using WCS.Helpers;
using WCS.Entity;
using WcsTask;
using WcsTask.DevCommTaks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
using SqlSugar;
using Sunny.UI.Win32;

namespace WCS.Controls.系统查询
{
    public partial class ChangeMaterialType : UIPage
    {
        public static ChangeMaterialType _ChangeMaterialType;
        public ChangeMaterialType()
        {
            InitializeComponent();
            _ChangeMaterialType = this;
        }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper SqlSugarDapper = SqlSugarDapper.GetDbContext();

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (layer9001.Text != "")
            {
                Change("9001", MaterialType9001.Text, layer9001.Text, isNull9001.Text);
            }
        }

        private void AddTask_Load(object sender, EventArgs e)
        {
            //Dictionary<string, string> PLCType = new Dictionary<string, string>();
            //foreach (var item in SqlSugarDapper.DB.Queryable<Entity.PalletToMaterial>().ToList().FindAll(f => !f.MaterialType.Contains("物料")))
            //{
            //    PLCType.Add(item.MaterialType, item.MaterialType);
            //}

            //MaterialType9001.DataSource = new BindingSource(PLCType, null);
            //MaterialType9001.DisplayMember = "key";
            //MaterialType9001.ValueMember = "value";

            //MaterialType9002.DataSource = new BindingSource(PLCType, null);
            //MaterialType9002.DisplayMember = "key";
            //MaterialType9002.ValueMember = "value";

            Re();
        }

        public void Re()
        {
            this.view9001.DataSource = SqlSugarDapper.DB.Queryable<Warehouse>().Where(w => w.WarehouseCode == "9001").OrderBy(o => o.layer).ToList();
            this.view9002.DataSource = SqlSugarDapper.DB.Queryable<Warehouse>().Where(w => w.WarehouseCode == "9002").OrderBy(o => o.layer).ToList();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (layer9002.Text != "")
            {
                Change("9002", MaterialType9002.Text, layer9002.Text, isNull9002.Text);
            }
        }

        public void Change(string WarehouseCode, string NextMaterialType, string layer, string isNull)
        {

            int IsFullMaterial = 0;

            if (isNull == "1" || isNull == "0")
            {
                IsFullMaterial = Convert.ToInt16(isNull);
            }
            else
            {
                IsFullMaterial = Convert.ToInt16(1);
            }
            //删除完成读取是否还有托盘
            if (WarehouseInfo.GetWarehouseInfoIndex(WarehouseCode + "-" + layer) == 1)
            {
                SqlSugarDapper.DB.Updateable<Warehouse>()
                    .SetColumns(s => s.MaterialType == NextMaterialType)
                    .SetColumns(s => s.IsIN == 1)
                    .SetColumnsIF(IsFullMaterial == 1 || IsFullMaterial == 0, s => s.IsFullMaterial == IsFullMaterial)
                    .Where(w => w.layer == layer && w.WarehouseCode == WarehouseCode).ExecuteCommand();
            }
            else
            {
                SqlSugarDapper.DB.Updateable<Warehouse>()
                    .SetColumns(s => s.NextMaterialType == NextMaterialType + "," + IsFullMaterial.ToString())
                    .SetColumns(s => s.IsIN == 0)
                    .Where(w => w.layer == layer && w.WarehouseCode == WarehouseCode).ExecuteCommand();
            }
            Re();
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            Re();
        }

        ChangeAllowInOut changeAllowInOut = null;
        private void uiButton4_Click(object sender, EventArgs e)
        {
            if (changeAllowInOut == null || changeAllowInOut.IsDisposed)
            {
                changeAllowInOut = new ChangeAllowInOut();
                changeAllowInOut.Show();
            }
            else
            {
                changeAllowInOut.Show();
            }
        }

        private void MaterialType9001_DropDown(object sender, EventArgs e)
        {
            var list = SqlSugarDapper.DB.Queryable<Entity.PalletToMaterial>()
               .ToList()
               .Where(w => !w.MaterialType.Contains("物料"))
               .OrderBy(w => w.MaterialType)
               .GroupBy(w => w.MaterialType)
               .ToList();

            MaterialType9001.Items.Clear();
            foreach (var item in list)
            {
                MaterialType9001.Items.Add(item.Key);
            }
            Re();

        }

        private void MaterialType9002_DropDown(object sender, EventArgs e)
        {
            var list = SqlSugarDapper.DB.Queryable<Entity.PalletToMaterial>()
               .ToList()
               .Where(w => !w.MaterialType.Contains("物料"))
               .OrderBy(w => w.MaterialType)
               .GroupBy(w => w.MaterialType)
               .ToList();

            MaterialType9002.Items.Clear();
            foreach (var item in list)
            {
                MaterialType9002.Items.Add(item.Key);
            }
            Re();
        }
    }
}
